const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');
const common = require('../../../../utils/common.js');
const app = getApp()
var _animation;
Page({
  data: {
    //一周的签到
    attendance: [{}, {}, {}, {}, {}, {}, {}],
    //用户橙贝数量
    cbSum: 0,
    //用户抽奖次数
    fpSum: 0,
    //是否开启翻牌
    openfanpai: false,
    //是否开启洗牌
    openxipai: true,
    //牌的翻转
    pai: [],
    //牌的动画数据
    ani:[],
    //今天星期几
    day: '',
    //当日是否签到
    tosign: false,
    //签到面板开关
    openSign: false,
    //分享成功面板开关
    openShare: false,
    //总签到天数
    sunDay: 0,
    //补签面板的开关
    openBuqian: false,
    //补签分享页面开关
    openyqBuqian: false,
    //获取补签的下标
    buqianIndex: '',
    //翻牌次数不足面板
    openPai: false,
    //提醒面板的开关
    openwarn: false,
    //提醒时间
    warntime: '07:00',
    //重复周期
    redo: [false, false, false, false, false, false, false, false],
    //关注公众号面板开关
    opengzh: false,
  },
  //加载
  onLoad: function(){
    var _this = this
    var today = new Date().getDay();
    if (today == 0) {
      var today = 7
    }
    _this.setData({
      day: today
    })
    if (app.globalData.logininfo != null && app.globalData.logininfo != '') {
      _this.setData({
        logininfo: app.globalData.logininfo,
        openid: app.globalData.logininfo.openid,
      });
    }
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        //获取一周的签到
        util.post(api.GetDailySign, data).then(function (res) {
          if (res.code == 200) {
            var arr = _this.data.attendance
            for (var i = 0; i < res.result.list.length;i++){
              var d = new Date(res.result.list[i].createdate.replace(/-/g, '/')).getDay();
              arr[d - 1] = res.result.list[i]
            }
            console.log(arr)
            _this.setData({
              attendance: arr
            })
            if (arr[_this.data.day - 1].type==1){
              _this.setData({
                tosign: true
              })
            }
            _this.setData({
              sunDay: res.result.obj
            })
            console.log(arr)
          }
        });
        //获取用户信息
        util.post(api.GetUserInfoByHomepage, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData: res.result.json,
            })
            _this.setData({
              fpSum: _this.data.userData.signFlipCardsCount,
              cbSum: _this.data.userData.enableIntegral
            })
          }
        });
      });
    }
  },
  //点击签到
  qiandao: function () {
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.DailySign, data).then(function (res) {
          if (res.code == 200) {
            console.log(res)
            _this.onLoad();
            _this.setData({
              openSign: true,
              openfanpai: true
            })
          }
        });
      });
    }
  },
  //签到分享
  fenxiang: function () {
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.ShareObtainOrangebei, data).then(function (res) {
          if (res.code == 200) {
            _this.onLoad();
            _this.closeMc();
          }
        });
      });
    }
  },
  //翻牌
  fanpai: function (e) {
    var _this = this
    var openid = app.globalData.logininfo.openid;
    _this.setData({
      openfanpai: false
    })
    var data = {};
    _this.audioCtx = wx.createAudioContext('myAudio')
    var pid = e.currentTarget.dataset.id
    var arr = _this.data.ani
    var openpai = _this.data.pai
    var count = _this.data.fpSum
    if (count != 0) {
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid;
          data.access_token = res.access_token;
          util.post(api.FlipCards, data).then(function (res) {
            if (res.code == 200) {
              _this.onLoad();
              _this.audioCtx.seek(0)
              _this.audioCtx.play()
              _this.setData({
                prizeImg: res.result.obj.prizeIp + res.result.obj.prizeImage,
                prizeText: res.result.obj.prizeNumber + res.result.obj.prizeUnit,
              })
              _animation = wx.createAnimation({
                duration: 300,
                timingFunction: 'linear',
              })
              _animation.rotateY(180).step()
              arr[pid] = _animation
              openpai[pid] = true
              _this.setData({
                ani: arr,
              })
              setTimeout(function () {
                _animation = wx.createAnimation({
                  duration: 0,
                  timingFunction: 'linear',
                })
                _animation.rotateY(0).step()
                arr[pid] = _animation
                _this.setData({
                  ani: arr,
                  pai: openpai,
                })
              }, 200)
            }
          });
        });
      }
    } else {
      _this.setData({
        openPai: true
      })
    }
  },
  //打开补签窗口
  buqian: function(e){
    var _this = this
    var bindex = e.currentTarget.dataset.index
    console.log(bindex)
    _this.setData({
      buqianIndex: bindex,
      openBuqian: true,
    })
  },
  //打开补签中窗口
  selyqBuqian: function(e){
    var _this = this
    var bindex = e.currentTarget.dataset.index
    _this.setData({
      openyqBuqian: true,
      buqianIndex: bindex
    })
  },
  //橙贝补签
  cbBuqian: function(){
    var _this = this
    var olbindex = _this.data.buqianIndex
    var newindex = _this.data.day-1
    var date = new Date();
    var newDate = date.getTime();
    var jian = (newindex - olbindex) * 86400000
    var buqianDate = new Date(newDate - jian);
    var nian =buqianDate.getFullYear();
    if (parseInt(buqianDate.getMonth() + 1)<10){
      var yue = "0" + parseInt(buqianDate.getMonth() + 1)
    }else{
      var yue = parseInt(buqianDate.getMonth() + 1)
    }
    if (buqianDate.getDate()< 10) {
      var ri = "0" + buqianDate.getDate()
    } else {
      var ri = buqianDate.getDate()
    }
    var newD = nian + "-" + yue + "-" + ri
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.type = 1;
        data.access_token = res.access_token;
        data.helpDate = newD;
        util.post(api.SupplementSign, data).then(function (res) {
          if (res.code == 200) {
            console.log(res)
            _this.onLoad();
            _this.closeMc();
          }else{
            wx.showToast({
              title: '补签失败',
              icon: 'none',
              duration: 2000
            })
          }
        });
      });
    }
  },
  //邀请补签
  yqBuqian: function(){
    var _this = this
    var bindex = _this.data.buqianIndex
    var arr = _this.data.attendance
    arr[bindex].type = 2
    this.setData({
      openyqBuqian: true,
      attendance: arr
    })
  },
  //关闭弹窗
  closeMc: function(){
    var _this = this
    _this.setData({
      openwarn: false,
      opengzh: false,
      openSign: false,
      openShare: false,
      openPai: false,
      openBuqian: false,
      openyqBuqian: false
    })
  },
  //发牌
  fapai: function () {
    var _this = this
    var arr = _this.data.ani
    _animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
    })
    _animation.top(84).left(28).step()
    arr[0] = _animation
    _this.setData({
      ani: arr
    })
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(84).left(108).step()
      arr[1] = _animation
      _this.setData({
        ani: arr
      })
    }, 100)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(84).left(188).step()
      arr[2] = _animation
      _this.setData({
        ani: arr
      })
    }, 200)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(84).left(268).step()
      arr[3] = _animation
      _this.setData({
        ani: arr
      })
    }, 300)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(229).left(28).step()
      arr[4] = _animation
      _this.setData({
        ani: arr
      })
    }, 400)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(229).left(108).step()
      arr[5] = _animation
      _this.setData({
        ani: arr
      })
    }, 500)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(229).left(188).step()
      arr[6] = _animation
      _this.setData({
        ani: arr
      })
    }, 600)
    setTimeout(function () {
      _animation = wx.createAnimation({
        duration: 300,
        timingFunction: 'linear',
      })
      _animation.top(229).left(268).step()
      arr[7] = _animation
      _this.setData({
        ani: arr
      })
    }, 700)
  },
  //收牌
  shoupai: function () {
    var _this = this
    var arr = _this.data.ani
    _animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'linear',
    })
    _animation.top(147).left(149).step()
    arr[0] = _animation
    arr[1] = _animation
    arr[2] = _animation
    arr[3] = _animation
    arr[4] = _animation
    arr[5] = _animation
    arr[6] = _animation
    arr[7] = _animation
    _this.setData({
      ani: arr
    })
  },
  //洗牌
  xipai: function () {
    var _this = this
    var openpai = _this.data.pai
    _this.setData({
      openfanpai: false,
      openxipai: false
    })
    _this.shoupai();
    setTimeout(function () {
      openpai[0] = false,
        openpai[1] = false,
        openpai[2] = false,
        openpai[3] = false,
        openpai[4] = false,
        openpai[5] = false,
        openpai[6] = false,
        openpai[7] = false,
        _this.setData({
          pai: openpai
        })
    }, 200)
    setTimeout(function () {
      _this.fapai();
    }, 500)
    setTimeout(function () {
      _this.setData({
        openfanpai: true,
        openxipai: true
      })
    }, 1500)
  },
  //加载
  onShow: function () {
    var _this = this    
    _this.shoupai();
    setTimeout(function () {
      _this.fapai();
    }, 500)
    setTimeout(function () {
      var sign = _this.data.tosign
      if (sign) {
        _this.setData({
          openfanpai: true
        })
      }
    }, 1500)
  },
  //分享
  onShareAppMessage(e) {
    console.log(e)
    var _this = this
    var type = e.target.dataset.type
    var olbindex = _this.data.buqianIndex
    var newindex = _this.data.day - 1
    var date = new Date();
    var newDate = date.getTime();
    var jian = (newindex - olbindex) * 86400000
    var buqianDate = new Date(newDate - jian);
    var nian = buqianDate.getFullYear();
    if (parseInt(buqianDate.getMonth() + 1) < 10) {
      var yue = "0" + parseInt(buqianDate.getMonth() + 1)
    } else {
      var yue = parseInt(buqianDate.getMonth() + 1)
    }
    if (buqianDate.getDate() < 10) {
      var ri = "0" + buqianDate.getDate()
    } else {
      var ri = buqianDate.getDate()
    }
    var newD = nian + "-" + yue + "-" + ri
    var openid = app.globalData.logininfo.openid;
    var date = new Date();
    var time = date.getTime();
    var data = {}
    data = { "helpDate": newD, "helpTime": time}
    var data = JSON.stringify(data)
    if (type==1){
      var url = '/pages/home/home'
    }else{
      var url = '/pages/home/shareJump/shareJump?type=3&isCheckLogin=1&shareOpenid=' + openid + '&data=' + data
    }
    console.log(url)
    return {
      title: '好玩的百科知识平台',
      desc: '',
      path: url,
    } 
  },
  //打开提醒面板
  showwarn: function () {
    wx.showToast({
      title: '功能暂未开放',
      icon: 'none',
      duration: 2000
    })
    /*var _this = this
    _this.setData({
      openwarn: true
    })*/
  },
  //关闭开提醒面板
  hidwarn: function () {
    var _this = this
    _this.setData({
      openwarn: false
    })
  },
  //获取设置提醒时间
  bindTimeChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      warntime: e.detail.value
    })
  },
  //设置重复周期
  selctRedoWarn: function (e) {
    var _this = this
    var sel = e.currentTarget.dataset.index
    var arr = _this.data.redo
    if (sel == 7) {
      if (arr[sel]) {
        for (var i = 0; i <= arr.length - 1; i++) {
          arr[i] = false
        }
        _this.setData({
          redo: arr
        })
      } else {
        for (var i = 0; i <= arr.length - 1; i++) {
          arr[i] = true
        }
        _this.setData({
          redo: arr
        })
      }
    } else {
      if (arr[sel]) {
        arr[sel] = false
      } else {
        arr[sel] = true
      }
      for (var i = 0; i < arr.length; i++) {
        if (arr[i] == false) {
          arr[7] = false
        }
      }
      _this.setData({
        redo: arr
      })
    }
  },
  //确定提醒
  confirmWarn: function () {
    var _this = this
    _this.setData({
      openwarn: false
    })
    wx.showToast({
      title: '设置成功',
      duration: 1500
    })
  },
  //去关注
  showgzh: function () {
    var _this = this
    _this.setData({
      openwarn: false,
      opengzh: true
    })
  },
})